// pages/newTopic/newTopic.js
var util=require('../../utils/util')
let nickName=""
let openid=""
// let image=[]
let land=[]
let state={}
let dateTime=""

Page({
  /**
   * 页面的初始数据
   */
  data: {
    type:"",
    array:["中药","方剂","实验","诊断","求助","答疑","班级"],
    imageList:[],
    image:[],
    title:"",
    context:"",
  },
  inputTitle(e)
  {
    let that=this
    that.setData({
      title:e.detail.value
    })
    console.log(that.data.title)
  },
  inputContent(e)
  {
    let that=this
    that.setData({
      context:e.detail.value
    })
    console.log(that.data.context)
  },
  bindPickChange(res)
  {
    let that=this
    var type=res.detail.value
    that.setData({
      type:that.data.array[type]
    })
  },
  addImage()
  {
    let that=this
    var imageList=that.data.imageList
    wx.chooseImage({
      count:1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: chooseResult => {
        var tempFilePaths=chooseResult.tempFilePaths
        // for(var i=0;i<tempFilePaths.length;i++)
        // {
        //   if (imageList.length >= 1) {
        //     that.setData({
        //       imageList: imageList
        //     });
        //     return false;
        //   } else {
            imageList.push(tempFilePaths[0]);
          // }
        // }
        this.setData({
          imageList:imageList
        })
        console.log(that.data.imageList)
      },
    })

  },
  deleteImg: function (e) {
    var imageList = this.data.imageList;
    var index = e.currentTarget.dataset.index;
    imageList.splice(index, 1);
    this.setData({
      imageList: imageList
    });
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imageList = this.data.imageList;
    wx.previewImage({
      //当前显示图片
      current: imageList[index],
      //所有图片
      urls: imageList
    })
  },
  async addTopic()
  {
    var TIME = util.formatTime(new Date());
    var time=util.formatTime2(new Date());
    dateTime=time
    const that=this
    var imageList=that.data.imageList
    // var image=that.data.image
    var len=imageList.length
    var title=that.data.title
    var type=that.data.type
    var context=that.data.context
    nickName=wx.getStorageSync('nickName')
    openid=wx.getStorageSync('openid')
    var userImage=wx.getStorageSync('url')
    // console.log(url)
    if (title==""||context=="") {
      wx.showToast({
        title: '标题或内容为空',
        icon:"none"
      })
    }
    else
    {
      if (!len==0) {
        // 将图片上传至云存储空间
        for(var i=0;i<len;i++)
        {
          wx.cloud.uploadFile({
            // 指定上传到的云路径
            cloudPath: './image/TopicList/'+TIME+i+'.png',
            // 指定要上传的文件的小程序临时文件路径
            filePath: imageList[i],
            // 成功回调
            success: res => {
              console.log('上传成功', res.fileID)
              // let index = res.fileID.lastIndexOf("/")
              that.setData({
                image: that.data.image.concat(res.fileID)
              })
              wx.showLoading({title: '发表中'})
              wx.cloud.callFunction({
                name:"updata",
                data:{
                  action:"topicList_add",
                  nickName:nickName,
                  userImage:userImage,
                  openid:openid,
                  land:land,
                  title:title,
                  context:context,
                  state:state,
                  dateTime:dateTime,
                  type:type,
                  image:that.data.image
                } 
              })
              wx.hideLoading()
              wx.navigateBack({
                complete: (res) => {},
              })
              console.log(that.data.image)
        }, 
      })
    }
      }
      else
      {
        wx.showLoading({
          title: '发表中',
        })
        wx.cloud.callFunction({
          name:"updata",
          data:{
            action:"topicList_add",
            nickName:nickName,
            userImage:userImage,
            openid:openid,
            image:that.data.image,
            land:land,
            title:title,
            type:type,
            dateTime:dateTime,
            context:context,
            state:state
          } 
        })
        wx.hideLoading()
        wx.navigateBack({
          complete: (res) => {},
        })
      }
      }
    }
})